﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bymed.SpineScan.DataModel;
using Bymed.SpineScan.Repository.Interface;

namespace Bymed.SpineScan.Repository.EntityFramework
{
    /// <summary>
    /// A factory class that manage every single repository.
    /// </summary>
    public class RepositoryFactory : IRepositoryFactory
    {
        private SpineScanContext _dataContext;

        /// <summary>
        /// Open a database connection without a transaction
        /// </summary>
        public void OpenSession()
        {
            _dataContext = new SpineScanContext();
        }

        /// <summary>
        /// Open a database connection with a transaction
        /// </summary>
        public void OpenSessionWithTransaction()
        {
            OpenSession();
        }

        /// <summary>
        /// Rollback the transaction
        /// </summary>
        public void Rollback() { }

        /// <summary>
        /// Commit the transaction
        /// </summary>
        public void Commit()
        {
            if (_dataContext != null)
            {
                _dataContext.SaveChanges();
            }
        }

        /// <summary>
        /// Flush current data
        /// </summary>
        public void Flush() { }

        /// <summary>
        /// Commit the transaction based on the provided parameter and close the database connection
        /// </summary>
        public void CloseSession()
        {
            if (_dataContext != null)
            {
                _dataContext.Dispose();
            }
        }

        public void Dispose()
        {
            CloseSession();
        }

        /// <summary>
        /// Create an instance of RepositoryBase against the specific DataModel type and provide standard CRUD operation only.
        /// </summary>
        /// <typeparam name="T">The provided DataModel type.</typeparam>
        /// <returns>An instance of RepositoryBase against the specific DataModel type and provide standard CRUD operation only.</returns>
        public IRepository<T> CreateRepositoryBase<T>() where T : class
        {
            return new RepositoryBase<T>(_dataContext);
        }

        /// <summary>
        /// Create an instance of AttendingDoctorRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of AttendingDoctorRepository</returns>
        public IAttendingDoctorRepository CreateAttendingDoctorRepository()
        {
            return new AttendingDoctorRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of DiagnosticMessageReferenceRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of DiagnosticMessageReferenceRepository</returns>
        public IDiagnosticMessageReferenceRepository CreateDiagnosticMessageReferenceRepository()
        {
            return new DiagnosticMessageReferenceRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of TestResultDiagnosticMessageRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of TestResultDiagnosticMessageRepository</returns>
        public ITestResultDiagnosticMessageRepository CreateTestResultDiagnosticMessageRepository()
        {
            return new TestResultDiagnosticMessageRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of PatientRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of PatientMessageRepository</returns>
        public IPatientRepository CreatePatientRepository()
        {
            return new PatientRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of TestResultRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of TestResultRepository</returns>
        public ITestResultRepository CreateTestResultRepository()
        {
            return new TestResultRepository(_dataContext);
        }

        /// <summary>
        /// Create an instance of TestTrackingRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of TestTrackingRepository</returns>
        public ITestTrackingRepository CreateTestTrackingRepository()
        {
            return new TestTrackingRepository(_dataContext);
        }

        //// <summary>
        /// Create an instance of CustomizedTestItemRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of CustomizedTestItemRepository</returns>
        public ICustomizedTestItemRepository CreateCustomizedTestItemRepository()
        {
            return new CustomizedTestItemRepository(_dataContext);
        }
    }
}
